﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Runtime.Serialization;

namespace Imageo
{
    public partial class TrailGMap : System.Web.UI.UserControl
    {
        MapData mapData = new MapData();
        List<bintje.fullTrail> currTrail;

        #region Properties

        public event EventHandler SubmitEvent;

        private bool canEdit;
        public bool CanEdit
        {
            get
            {
                return canEdit;
            }

            set
            {
                canEdit = value;
            }
        }

        private bool easyMode;
        public bool EasyMode
        {
            get
            {
                return easyMode;
            }
            set
            {
                easyMode = value;
            }
        }

        public Unit Width
        {
            set
            {
                pnlMapControl.Width = value;
            }
        }

        public Unit Height
        {
            set
            {
                pnlMapControl.Height = value;
            }
        }
        
        int? trailId;

        public List<bintje.fullTrail> CurrTrail
        {
            get { return JSONHelper.Deserialize<List<bintje.fullTrail>>(hfJsonTrailData.Value); }
        }

        #endregion

        #region MapData container

        [DataContract]
        public class MapData
        {
            [DataMember]
            public int centerLat;           // map center lat
            [DataMember]
            public int centerLon;           // map center lon
            [DataMember]
            public int zoomLevel;           // map zoom

            [DataMember]
            public bool individualTrailMode;  // individual trail is shown
            [DataMember]
            public int trailId;             // Selected trail
            [DataMember]
            public bool canEdit;            // You can edit this trail
            [DataMember]
            public bool easyMode;           // Only one waypoint in easy mode
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            string strProUserId = Request.QueryString["prouserid"] as string;
            int proUserId = 0;

            if (!string.IsNullOrEmpty(strProUserId))
            {
                if (!int.TryParse(strProUserId, out proUserId))
                {
                    Response.Redirect("~/UI/ProErrorGeneric.aspx");
                }
                hfProUserId.Value = "&prouserid=" + proUserId.ToString();
            }

            // Check all values are ok or put default values
            if (hfJsonTrailData.Value == "")
            {
                IList<bintje.trailWrapper> trailList = new List<bintje.trailWrapper>();
                // Serialize the complete trailList into the hidden field
                hfJsonTrailData.Value = JSONHelper.Serialize<IList<bintje.trailWrapper>>(trailList);
            }

            if (hfJsonMapData.Value == "")
            {
                // Prepare other map parameters
                mapData.canEdit = this.canEdit;
                mapData.easyMode = this.EasyMode;
                // Serialize other map parameters for the map
                hfJsonMapData.Value = JSONHelper.Serialize<MapData>(mapData);
            }
            SetLayout();
        }

        void SetLayout()
        {
            pnlMapSide.Visible=  this.canEdit;
            pnlTools.Visible = this.canEdit;
        }

        public void LoadTrails(IList<bintje.trailWrapper> trailList)
        {
            // If the trail list is null, create an empty list
            if (trailList == null)
                trailList = new List<bintje.trailWrapper>();

            // TODO: remove invalid trails
            List<bintje.trailWrapper> tmpTrails = new List<Imageo.bintje.trailWrapper>();
            foreach (bintje.trailWrapper tw in trailList)
            {
                if (tw.latitude != 0 && tw.longitude != 0)
                {
                    tmpTrails.Add(tw);
                }
            }

            // Serialize the complete trailList into the hidden field
            hfJsonTrailData.Value=JSONHelper.Serialize<IList<bintje.trailWrapper>>(tmpTrails);
        }

        public void LoadFullTrail(bintje.fullTrail fullTrail)
        {
            mapData.individualTrailMode = true;
            mapData.trailId = fullTrail.id;
            // Serialize the complete trailList into the hidden field
            IList<bintje.fullTrail> tmpList = new List<bintje.fullTrail>();
            tmpList.Add(fullTrail);

            hfJsonTrailData.Value = JSONHelper.Serialize<IList<bintje.fullTrail>>(tmpList);
        }

        public bintje.fullTrail CreateNewEmptyTrail()
        {
            mapData.individualTrailMode = true;
            mapData.trailId = 0;                    // new trail

            bintje.fullTrail fullTrail = new Imageo.bintje.fullTrail();
            fullTrail.created = DateTime.Today;
            fullTrail.publicTrail = true;

            IList<bintje.fullTrail> tmpList = new List<bintje.fullTrail>();
            tmpList.Add(fullTrail);

            hfJsonTrailData.Value = JSONHelper.Serialize<IList<bintje.fullTrail>>(tmpList);

            return fullTrail;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (SubmitEvent != null)
                SubmitEvent(this, null);
        }
    }
}